using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Storage;
using Zune;
using TetraEngine;

namespace Tetrazune
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TetraZune : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Zune.Zune zune;
        TetraGameManager tgm;
        TimeSpan remainder = TimeSpan.Zero;
        TitleScreen titleScreen;

        public TetraZune()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            zune = new Zune.Zune(this);
            tgm = new TetraGameManager(this, zune, new ControlMapping());
            tgm.Enabled = false;
            tgm.Visible = false;
            titleScreen = new TitleScreen(this, zune);
            this.Components.Add(zune);
            this.Components.Add(tgm);
            this.Components.Add(titleScreen);
            titleScreen.GameStart += new EventHandler(titleScreen_GameStart);
            tgm.GameComplete += new EventHandler(tgm_GameComplete);
            base.Initialize();

            //This must be called after base.Initialize() muy importante!
            zune.Orientation = ZuneOrientations.Landscape;
        }

        void tgm_GameComplete(object sender, EventArgs e)
        {
            titleScreen.Enabled = true;
            titleScreen.Visible = true;
            tgm.Reset();
            tgm.Enabled = false;
            tgm.Visible = false;
        }

        void titleScreen_GameStart(object sender, EventArgs e)
        {
            tgm.Enabled = true;
            tgm.Visible = true;
            titleScreen.Enabled = false;
            titleScreen.Visible = false;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            base.LoadContent();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed && 
                tgm.Enabled == true)
                this.Exit();
            //GamePadState st = GamePad.GetState(PlayerIndex.One);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            zune.BeginDrawing();
            base.Draw(gameTime);
            zune.EndDrawing();
            zune.Draw();
        }
    }
}
